import { Category } from '../vendor/order_food/category';
import { GoodsList } from '../vendor/order_food/goods';
import { Order } from '../vendor/order_food/order';
import { Shop, init as shopInit } from '../vendor/shop/shop';
import { reactive } from 'vue';
// 初始化数据
export type Goods = {
	id: number;
	title: string;
	price: number;
	number: number;
	img: string;
	cate_id: number;
};

export type State = {
	cart: Array<Goods>;

	address: any;
	addresses: any;

	order: any;
	busOrderList: Array<Order>; // 商户订单列表

	shop: Shop;
	goodsList: Array<GoodsList>; //  商户端商品商品列表
	categoryList:Array<Category> // 分类列表
	
};

export const ORDER_SUCCESS = 2; // 支付成功
export const ORDER_DELIVERY = 8; // 开始送货
export const ORDER_FINISHED = 5; // 送货完成

export const state = reactive({ shop: shopInit, cart: [], address: null, addresses: [], order: {}, busOrderList: [], goodsList: [],categoryList:[] } as State);

export const getCart = () => {
	if (state.cart.length) {
		return state.cart;
	} else {
		const cart = uni.getStorageSync('cart');
		if (cart) {
			state.cart = JSON.parse(cart);
		}
		return state.cart;
	}
};

export const setCart = (goods: Goods) => {
	state.cart.push(goods);
	uni.setStorage({
		key: 'cart',
		data: JSON.stringify(state.cart)
	});
};

export const setEmptyCart = () => {
	uni.setStorage({
		key: 'cart',
		data: ''
	});
	state.cart.length = 0;
};
